package game.module.offline.logic;

import java.util.Collection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import game.entity.PlayingRole;
import game.module.offline.bean.PlayerOfflineCache;
import game.module.user.bean.PlayerBean;
import game.module.user.dao.PlayerDao;

public class PlayerOfflineManager {

	private static Logger logger = LoggerFactory.getLogger(PlayerOfflineManager.class);

	public static final int MAX_PLAYER_CACHE_SIZE = 5000;

	static class SingletonHolder {
		static PlayerOfflineManager instance = new PlayerOfflineManager();
	}

	public static PlayerOfflineManager getInstance() {
		return SingletonHolder.instance;
	}

	private Cache<Integer, PlayerOfflineCache> playerOfflineMap = CacheBuilder.newBuilder().concurrencyLevel(8)
			.maximumSize(MAX_PLAYER_CACHE_SIZE).build();

	public void init() {
		List<PlayerBean> playerBeans = PlayerDao.getInstance().getPlayersForOffline(MAX_PLAYER_CACHE_SIZE);
		logger.info("offline players,size={}", playerBeans.size());
		for (int i = playerBeans.size() - 1; i >= 0; i--) {
			PlayerBean playerBean = playerBeans.get(i);
			PlayerOfflineCache playerOfflineCache = new PlayerOfflineCache();
			playerOfflineCache.setId(playerBean.getId());
			playerOfflineCache.setAccountId(playerBean.getAccountId());
			playerOfflineCache.setName(playerBean.getName());
			playerOfflineCache.setFang_ka(playerBean.getFang_ka());
			playerOfflineCache.setCoins(playerBean.getCoins());
			playerOfflineCache.setLevel(playerBean.getLevel());
			playerOfflineCache.setHeadimg(playerBean.getHeadimg());
			playerOfflineCache.setSex(playerBean.getSex());
			playerOfflineCache.setYuan_bao(playerBean.getYuan_bao());
			playerOfflineCache.setDownline_time(playerBean.getDownlineTime());
			playerOfflineCache.setJindou(playerBean.getJin_dou());
			playerOfflineMap.put(playerBean.getId(), playerOfflineCache);
		}
	}

	public void clear() {
		playerOfflineMap.invalidateAll();
	}

	public PlayerOfflineCache getPlayerOfflineCache(int playerId) {
		return playerOfflineMap.getIfPresent(playerId);
	}

	public PlayerOfflineCache getPlayerOfflineCache(String name) {
		Collection<PlayerOfflineCache> offlinePlayers = playerOfflineMap.asMap().values();
		for (PlayerOfflineCache offlineCache : offlinePlayers) {
			if (offlineCache.getName().equals(name)) {
				return offlineCache;
			}
		}
		return null;
	}

	public long getPlayerOfflineCacheSize() {
		return playerOfflineMap.size();
	}

	public boolean checkExist(Integer id) {
		return playerOfflineMap.getIfPresent(id) == null ? false : true;
	}

	public void addPlayerOfflineCache(int playerId, PlayerOfflineCache cache) {
		playerOfflineMap.put(playerId, cache);
	}

	public void loadFromDb(Integer playerId) {
		PlayerBean playerBean = PlayerDao.getInstance().getPlayerById(playerId);
		PlayerOfflineCache playerOfflineCache = new PlayerOfflineCache();
		playerOfflineCache.setId(playerBean.getId());
		playerOfflineCache.setAccountId(playerBean.getAccountId());
		playerOfflineCache.setName(playerBean.getName());
		playerOfflineCache.setFang_ka(playerBean.getFang_ka());
		playerOfflineCache.setCoins(playerBean.getCoins());
		playerOfflineCache.setLevel(playerBean.getLevel());
		playerOfflineCache.setHeadimg(playerBean.getHeadimg());
		playerOfflineCache.setSex(playerBean.getSex());
		playerOfflineCache.setYuan_bao(playerBean.getYuan_bao());
		playerOfflineCache.setDownline_time(playerBean.getDownlineTime());
		playerOfflineCache.setJindou(playerBean.getJin_dou());
		playerOfflineMap.put(playerBean.getId(), playerOfflineCache);
	}

	public void updatePlayerOffline(PlayingRole playingRole, PlayerOfflineCache playerOfflineCache) {
		PlayerBean playerBean = playingRole.getPlayerBean();
		playerOfflineCache.setId(playerBean.getId());
		playerOfflineCache.setAccountId(playerBean.getAccountId());
		playerOfflineCache.setName(playerBean.getName());
		playerOfflineCache.setFang_ka(playerBean.getFang_ka());
		playerOfflineCache.setCoins(playerBean.getCoins());
		playerOfflineCache.setLevel(playerBean.getLevel());
		playerOfflineCache.setHeadimg(playerBean.getHeadimg());
		playerOfflineCache.setSex(playerBean.getSex());
		playerOfflineCache.setYuan_bao(playerBean.getYuan_bao());
		playerOfflineCache.setDownline_time(playerBean.getDownlineTime());
		playerOfflineCache.setJindou(playerBean.getJin_dou());
	}

	public Collection<PlayerOfflineCache> getPlayerOfflineAll() {
		return playerOfflineMap.asMap().values();
	}

	public void addOrUpdatePlayerOffline(PlayerBean playerBean) {
		int playerId = playerBean.getId();
		PlayerOfflineCache playerOfflineCache = playerOfflineMap.getIfPresent(playerId);
		if (playerOfflineCache == null) {
			playerOfflineCache = new PlayerOfflineCache();
			playerOfflineMap.put(playerBean.getId(), playerOfflineCache);
		}
		playerOfflineCache.setId(playerBean.getId());
		playerOfflineCache.setAccountId(playerBean.getAccountId());
		playerOfflineCache.setName(playerBean.getName());
		playerOfflineCache.setFang_ka(playerBean.getFang_ka());
		playerOfflineCache.setCoins(playerBean.getCoins());
		playerOfflineCache.setLevel(playerBean.getLevel());
		playerOfflineCache.setHeadimg(playerBean.getHeadimg());
		playerOfflineCache.setSex(playerBean.getSex());
		playerOfflineCache.setYuan_bao(playerBean.getYuan_bao());
		playerOfflineCache.setDownline_time(playerBean.getDownlineTime());
		playerOfflineCache.setJindou(playerBean.getJin_dou());
	}

}
